package com.lianniu.mgr.product.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.constants.ErrorCode;
import com.lianniu.mgr.pagination.PaginationInfo;
import com.lianniu.mgr.product.dao.IProductBrandDao;
import com.lianniu.mgr.product.model.ProductBrandVo;
import com.lianniu.mgr.product.service.IProductBrandService;
import com.lianniu.mgr.util.StringUtil;

@Service
public class ProductBrandServiceImpl implements IProductBrandService {

	private final static Logger logger = Logger.getLogger(ProductBrandServiceImpl.class);
	  
	@Resource
	IProductBrandDao productBrandDao;
	
	@Override
	public BaseResult insertProductBrand(ProductBrandVo curProductBrandVo) {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
	    logger.info("insertProductBrand begin");
	    logger.info("insertProductBrand: input data check begin");
	    logger.info("insertProductBrand curProductBrandVo:" +JSON.toJSONString(curProductBrandVo));
	   
	    
	    BaseResult baseResult;
	    if (StringUtil.isEmpty(curProductBrandVo.getCateName())) {
	      baseResult = BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
	      return baseResult;
	    }	    
	    if (StringUtil.isEmpty(curProductBrandVo.getSortNum())) {
	      baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
	      return baseResult;
	    }  
	    if (StringUtil.isEmpty(curProductBrandVo.getIsBottom())) {
	    	curProductBrandVo.setIsBottom("0");
		  }  
		    
	    if (StringUtil.isEmpty(curProductBrandVo.getIsValidate())) {
	    	curProductBrandVo.setIsValidate("0");
		  }      
	    
	    logger.info("insertProductBrand: input data check end");
	    
	    return BaseResult.success(productBrandDao.insertProductBrand(curProductBrandVo));
	}

	@Override
	public BaseResult selectByProductBrandForPage(ProductBrandVo curProductBrandVo, PaginationInfo paginationInfo) {
		  // TODO Auto-generated method stub
	    logger.info("selectByProductBrandForPage begin.");
	    if (!StringUtil.isEmpty(curProductBrandVo.getCateName())) {
	    	curProductBrandVo.setCateName(curProductBrandVo.getCateName().replace(" ", ""));
	    }
	    logger.info("selectByProductBrandForPage BrandName:" +curProductBrandVo.getCateName());
	    if (!StringUtil.isEmpty(curProductBrandVo.getParentId())) {
	    	curProductBrandVo.setParentId(curProductBrandVo.getParentId().replace(" ", ""));
	    }
	    logger.info("selectByProductBrandForPage parentId:" +curProductBrandVo.getParentId());
	    if (!StringUtil.isEmpty(curProductBrandVo.getCateId())) {
	    	curProductBrandVo.setCateId(curProductBrandVo.getCateId().replace(" ", ""));
	    }
	    logger.info("selectByProductBrandForPage CateId:" +curProductBrandVo.getCateId());
	    logger.info("selectByProductBrandForPage paginationInfo:" +JSON.toJSONString(paginationInfo));
	    
	    curProductBrandVo.setPaginationInfo(paginationInfo);
	    List<ProductBrandVo> list=productBrandDao.selectByProductBrandForPage(curProductBrandVo);
	    Map<String,Object> retmap=new HashMap<String,Object>();
	    retmap.put("total", paginationInfo.getTotalRecord());
	    retmap.put("curPage", paginationInfo.getCurPage());
	    retmap.put("rows", list);
	    logger.info("selectByProductBrandForPage:  end");    
	    
	    return BaseResult.success(retmap); 
	}

	@Override
	public BaseResult updateById(ProductBrandVo curProductBrandVo) {
		// TODO Auto-generated method stub
	    logger.info("updateById begin");
	    logger.info("updateById: update data check begin");
	    logger.info("updateById curProductBrand:" +JSON.toJSONString(curProductBrandVo));
	  
	    
	    BaseResult baseResult;
	    if (StringUtil.isEmpty(curProductBrandVo.getCateName())) {
	      baseResult =BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
	      return baseResult;
	    }
	    if (StringUtil.isEmpty(curProductBrandVo.getSortNum())) {
	      baseResult = BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
	      return baseResult;
	    }
	    if (StringUtil.isEmpty(curProductBrandVo.getIsBottom())) {
	    	curProductBrandVo.setIsBottom("0");
		}
	   
	    
	    logger.info("updateById: update data check end");
	    
	    return BaseResult.success(productBrandDao.updateById(curProductBrandVo));
	}

	@Override
	public BaseResult deleteById(String Id) {
		// TODO Auto-generated method stub
		 logger.info("deleteById begin");
		 logger.info("deleteById Id:" +Id);
		  
		 return BaseResult.success(productBrandDao.deleteById(Id));
	}

	@Override
	public BaseResult selectById(ProductBrandVo curProductBrandVo) {
		 // TODO Auto-generated method stub
	    logger.info("selectById begin");
	    logger.info("selectById Id:" +curProductBrandVo.getCateId());
	  
	    return BaseResult.success(productBrandDao.selectById(curProductBrandVo.getCateId()));
	}

	@Override
	public Boolean checkCateName(String cateName) {
		// TODO Auto-generated method stub
	    logger.info("checkCateName begin");
	    logger.info("checkCateName cateName:" +cateName.toString());
	    logger.info("checkCateName: input data check begin");   
	  
	    if (StringUtil.isEmpty(cateName)) {       
	      return false;
	    }
	    logger.info("checkCateName: input data check end");
	    
	    ProductBrandVo productBrand=new ProductBrandVo();	   
	    productBrand.setCateName(cateName);
	    int count=productBrandDao.count(productBrand);
	    if(count>0){
	      return false;      
	    }
	    else{
	      return true;
	    }   
	}

	@Override
	public BaseResult deleteProductBrandBatch(String... id) {
		// TODO Auto-generated method stub
	    logger.info("deleteProductBrandBatch begin");
	    logger.info("deleteProductBrandBatch Ids:" +id.toString());
	    logger.info("deleteProductBrandBatch: input data check begin");
	   
	    BaseResult baseResult;
	    if (StringUtil.isEmpty(id.toString())) {
	      baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
	      return baseResult;
	    }
	    logger.info("deleteProductBrandBatch: input data check end");
	    
	    return BaseResult.success(productBrandDao.deleteProductBrandBatch(id));
	}

	@Override
	public BaseResult selectParentBrand(String cateId) {
		logger.info("selectParentBrand begin");
	    logger.info("selectParentBrand parm: null");
	  
	    return BaseResult.success(productBrandDao.getProductBrandKeyValue(cateId));
	}

	@Override
	public BaseResult selectChildBrandKV(String parentId) {
		// TODO Auto-generated method stub
	    logger.info("selectChildBrandKV begin");
	    logger.info("selectChildBrandKV parm:"+parentId);
	  
	    return BaseResult.success(productBrandDao.getProductBrandChildKv(parentId));
	}

}
